<%+header%>
<!--
    This module gives some convinience to access the shell via web, in
    case that you don't have a uart console or telnet/ssh connection.
    Hua Shao <nossiac@163.com>
-->

<script src="/luci-static/resources/monCon.js"></script>

<h2><a name="content">Web Console</a></h2>
<form id="console">
    <fieldset class="cbi-section">
        <legend>Execute shell commands or scripts as root. <span style="color:red">Be Careful</span>.</legend>
        <p>Press <b>Enter</b> to execute. Press <b>Shift+Enter</b> to start a new line.</p>
        <p><textarea name="cmd" id="cmd" style="width:98%;height:4em;"></textarea></p>
        <p>
            <button class="cbi-button" onclick="return postcmd('iwconfig')">iwconfig</button>
            <button class="cbi-button" onclick="return postcmd('ifconfig -a')">ifconfig -a</button>
            <button class="cbi-button" onclick="return postcmd('cat /proc/meminfo')">meminfo</button>
            <button class="cbi-button" onclick="return postcmd('switch dump; switch vlan dump')">switch</button>
            <button class="cbi-button" onclick="return postcmd('uci show')">uci</button>
            <button class="cbi-button" onclick="return postcmd('ps w')">ps</button>
            <button class="cbi-button" onclick="return postcmd('cat /proc/mtd')">mtd</button>
            <button class="cbi-button" onclick="return postcmd('block info')">block info</button>
            <button class="cbi-button" onclick="return postcmd('mount')">mount</button>
            <button class="cbi-button" onclick="return postcmd('cat /etc/kernel.config')">/etc/kernel.config</button>
            <button class="cbi-button" onclick="return postcmd('cat /etc/openwrt.config')">/etc/openwrt.config</button>
        </p>
        <pre id="result" style="background-color:black;color:white;height:auto;min-height:200px;width:98%;"></pre>
    </fieldset>
</form>

<script type="text/javascript">

    function postcmd(cmd) {
        (new XHR()).post("<%=luci.dispatcher.build_url("admin", "mtk", "webcmd")%>", {"cmd":cmd}, function(x) {
            console.log(x.response)
            console.log(x)
            document.getElementById("result").innerHTML = x.response;
        });
        return false;
    }

    document.getElementById("cmd").addEventListener("keydown", function(e) {
        if (!e) { var e = window.event; }

        if (e.keyCode == 13 && !e.shiftKey) {
            e.preventDefault();
            var cmd = document.getElementById("cmd");
            postcmd(cmd.value);
            cmd.value = "";
            return true;
        }
    }, false);

    function get_apply_status_cb(rsp)
    {
        try{
            var r = JSON.parse(rsp);
        }
        catch(e){
            return;
        }
        if(r.status == "ON_PROGRESS"){
            var u = confirm("Device is applying the saved settings now!\n" +
                "It is recommended to wait until all the saved settings are applied.\n" +
                "Please click on 'OK' button to wait for the device.\n" +
                "or click on 'Cancel' button to configure the settings available in current web-page.");
            if (u == true) {
                location = '<%=luci.dispatcher.build_url("admin", "mtk", "wifi", "loading")%>' + window.location.pathname;
            }
        }
    }

    function get_apply_status()
    {
        XHR.get('<%=luci.dispatcher.build_url("admin", "mtk", "wifi", "get_apply_status")%>', null,
            function(x)
            {
                console.log(x);
                get_apply_status_cb(x.response);
            }
        );
    }

    window.onload = function(){
        MonCon.ping();
        get_apply_status();
    }

</script>

<%+footer%>

